/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aliasMatching.controller;

import java.awt.Dimension;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;

import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author amendrashrestha
 *
 * This class creates graph by taking post time for users.
 */
public class CreateGraph {

    static List clickedUser;

    public JPanel createDemoPanel(List user) {
        try {
            JPanel graphPane = new JPanel();
            JFreeChart jfreechart = createTimeChart(createTimeDataset(user));
            ChartPanel chartpanel = new ChartPanel(jfreechart);
            chartpanel.setMouseWheelEnabled(true);
            chartpanel.setPreferredSize(new Dimension(480, 260));
            graphPane.add(chartpanel);
            //scroller.getModel().addChangeListener(chartpanel);
            return graphPane;
        } catch (Exception eee) {
            return null;
        }
    }

    public JPanel createTimegraphPanel(List clickedUser) {
        try {
            JFreeChart jfreechart = createTimeChart(createTimeDataset(clickedUser));
            ChartPanel chartpanel = new ChartPanel(jfreechart);
            chartpanel.setPreferredSize(new Dimension(490, 260));
            //scroller.getModel().addChangeListener(chartpanel);
            return chartpanel;
        } catch (Exception eee) {
            return null;
        }
    }

    private static JFreeChart createTimeChart(CategoryDataset xydataset) {
        JFreeChart jfreechart = ChartFactory.createBarChart("User Posts", "Hour", "Posts", xydataset,
                PlotOrientation.VERTICAL, true, true, false);
        return jfreechart;
    }

    private CategoryDataset createTimeDataset(List clickedUser) throws SQLException {
        DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
        HashMap<Integer, int[]> map = new HashMap<Integer, int[]>();

        int max_count = 0;

        for (int i = 0; i < clickedUser.size(); i++) {
            int ClickedUser = Integer.parseInt(clickedUser.get(i).toString());
            List o = TimeEquality.getPostTime(ClickedUser);

            Iterator<Object> itr = o.iterator();
            int[] rr = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            while (itr.hasNext()) {
                Timestamp key = (Timestamp) itr.next();
                int hr = key.getHours();
                
                if (map.containsKey(ClickedUser)) {                  
                    rr[hr]++;
//                    if (rr[hr] > max_count) {
//                        max_count = rr[hr];
//                    }

                    map.put(ClickedUser, rr);
                } else {
                    rr[hr]++;
                    map.put(ClickedUser, rr);
                }
            }
        }

        Iterator<Integer> kitr = map.keySet().iterator();
        while (kitr.hasNext()) {
            Integer k = kitr.next();
            int[] post_count = map.get(k);
            int totalPostCount = post_count.length;
            int userPostSize = 0;

            //getting total number of post of each users
            for(int i : post_count){
                userPostSize += i;
            }

            for (Integer i = 0; i < totalPostCount; ++i) {
                defaultcategorydataset.addValue((double) post_count[i] * 1.0 / userPostSize, k, i);
            }
        }
        return defaultcategorydataset;
    }
}